import {Request, Response, NextFunction, Router} from "express";
import {Repository} from "sequelize-typescript";
import {validationResult, body, check} from 'express-validator'
import {Active} from "../models";
import {checkToken} from "../middlewares/jwt";

interface BodyRequestBody extends Request {
    body: {
        [key: string]: string | undefined
    }
}


const activeRouterFactory = (
    activeRepository: Repository<Active>,
    ) => Router()
        .get('/all', async (req: BodyRequestBody, res: Response, next: NextFunction) => {
                let {page = 1, pageSize = 10} = req.query

                // console.log(filter)
                try {
                    let list = await activeRepository.findAndCountAll({
                        limit: Number(pageSize),
                        offset: (Number(page) - 1) * Number(pageSize)
                    })
                    if (list) {
                        return res.json({
                            code: 1,
                            msg: '动态数据获取成功',
                            data: list.rows,
                            meta: {
                                current_page: page,
                                per_page: pageSize,
                                count: list.count,
                                total: list.count,
                                total_pages: Math.ceil(list.count / Number(pageSize))
                            }
                        })
                    }
                    res.json({
                        errCode: 10001,
                        msg: '数据获取失败'
                    })
                } catch (e) {
                    console.log(e);
                    res.json({
                        errCode: 10001,
                        msg: '数据获取失败'
                    })
                    next()

                }
            }
        )
    .get('/detail',  check('id').isLength({min: 1}),async (req: BodyRequestBody, res: Response, next: NextFunction) => {

            try {
                let list = await activeRepository.findOne({
                    where:{
                        id: Number(req.query.id)
                    }
                })
                if (list) {
                    return res.json({
                        code: 1,
                        msg: '动态数据获取成功',
                        data: list,
                    })
                }
                res.json({
                    errCode: 10001,
                    msg: '数据获取失败'
                })
            } catch (e) {
                console.log(e);
                res.json({
                    errCode: 10001,
                    msg: '数据获取失败'
                })
                next()

            }
        }
    )
        .post('/create', checkToken, [
                body('title').isLength({min: 1}),
                body('act_author').isLength({min: 1}),
                body('content').isLength({min: 1}),
                body('act_QCode').isLength({min: 1}),
                body('act_pic').isLength({min: 1})
            ], async (req: BodyRequestBody, res: Response, next: NextFunction) => {
                const error = validationResult(req)
                if (!error.isEmpty()) {
                    return res.status(422).json({
                        errCode: 10000,
                        msg: error.array()
                    })
                }
                try {

                    const list = await activeRepository.create({
                        title: req.body.title,
                        act_author:req.body.act_author,
                        content: req.body.content,
                        act_QCode: req.body.act_QCode,
                        act_pic: req.body.act_pic
                    })
                    if (list) {
                        console.log('--- 活动发布成功 ---');
                        return res.json({
                            code: 1,
                            msg: '活动发布成功'
                        })
                    }
                    res.json({
                        errCode: 10001,
                        msg: '操作失败'
                    })
                } catch (e) {
                    console.log(e);
                    res.json({
                        errCode: 10001,
                        msg: '操作失败'
                    })
                    next()
                }
            }
        )
        .post('/del', checkToken, [
            body('id').isLength({min: 1})
        ], async (req: BodyRequestBody, res: Response, next: NextFunction) => {
            const error = validationResult(req)
            if (!error.isEmpty()) {
                return res.status(422).json({
                    errCode: 10000,
                    msg: error.array()
                })
            }
            let id = req.body.id
            // console.log(id,typeof id);
            try {
                if (id) {
                    const list = await activeRepository.destroy({
                        where: {
                            id: id
                        }
                    })
                    console.log(list)
                    if (list) {
                        console.log('--- 删除操作成功 ---');
                        return res.json({
                            code: 1,
                            msg: '删除成功'
                        })
                    }
                }
                return res.json({
                    errCode: 10001,
                    msg: '删除失败'
                })
            } catch (e) {
                console.log(e);
                res.json({
                    errCode: 10001,
                    msg: '操作失败'
                })
                next()
            }
        })

;
export default activeRouterFactory
